import { YhBaseEntity } from "@/common/baseentity";
import { Column, Entity, JoinColumn, ManyToOne, OneToMany } from "typeorm";
import { SysMenu, SysAction, SysUser } from "@/sys/entities/sys.entity";


@Entity({
    name: 'sys_role'
})
export class SysRole extends YhBaseEntity {
    @Column({
        type: 'varchar',
        name: 'name',
        comment: '角色名称',
        unique: true,
    })
    name: string;

    @OneToMany(() => SysRoleMenu, (rolemenu) => rolemenu.role)
    rolemenus: SysRoleMenu[];

    @OneToMany(() => SysRoleMenuAction, (roleaction) => roleaction.role)
    roleactions: SysRoleMenu[];

    @OneToMany(() => SysRoleUser, (roleuser) => roleuser.role)
    roleusers: SysRoleUser[];
}

@Entity({
    name: 'sys_roleuser'
})
export class SysRoleUser extends YhBaseEntity {
    @ManyToOne(() => SysRole, (role) => role.roleusers)
    @JoinColumn({
        name: 'roleId'
    })
    role: SysRole;

    @ManyToOne(() => SysUser, (user) => user.roleusers)
    @JoinColumn({
        name: 'userId'
    })
    user: SysUser;
}


@Entity({
    name: 'sys_rolemenu'
})
export class SysRoleMenu extends YhBaseEntity {
    @ManyToOne(() =>  SysRole, (role) => role.rolemenus)
    @JoinColumn({
        name: 'roleId'
    })
    role: SysRole;

    @ManyToOne(() => SysMenu, (menu) => menu.rolemenus)
    @JoinColumn({
        name: 'menuId'
    })
    menu: SysMenu;
}

@Entity({
    name: 'sys_rolemenuaction'
})
export class SysRoleMenuAction extends YhBaseEntity {
    @ManyToOne(() => SysRole, (role) => role.roleactions)
    @JoinColumn({
        name: 'roleId'
    })    
    role: SysRole;

    @ManyToOne(() => SysMenu, (menu) => menu.menuactions)
    @JoinColumn({
        name: 'menuId'
    })
    menu: SysMenu;

    @ManyToOne(() => SysAction, (action) => action.roleactions)
    @JoinColumn({
        name: 'actionId'
    })
    action: SysAction;
}
